package com.jimmy.reggie.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jimmy.reggie.common.R;
import com.jimmy.reggie.entity.Employee;
import com.jimmy.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Slf4j
@RestController
@RequestMapping("/employee")
public class   EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    /**
     *  登录：
     *  使用mybatis-plus
     *  将页面提交的密码password进行md5加密处理, 得到加密后的字符串
     *  根据页面提交的用户名username查询数据库中员工数据信息
     *  如果没有查询到, 则返回登录失败结果
     *  密码比对，如果不一致, 则返回登录失败结果
     *  查看员工状态，如果为已禁用状态，则返回员工已禁用结果
     *  登录成功，将员工id存入Session, 并返回登录成功结果
     * @param request
     * @param employee
     * @return
     */
     @PostMapping("/login")
     public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
         String password = employee.getPassword();
         password = DigestUtils.md5DigestAsHex(password.getBytes());
         LambdaQueryWrapper<Employee> employeeLambdaQueryWrapper = new LambdaQueryWrapper<Employee>();
         employeeLambdaQueryWrapper.eq(Employee::getUsername, employee.getUsername());
         Employee one = employeeService.getOne(employeeLambdaQueryWrapper);
         //这种代码风格，精准而优雅
         if (one==null){
             return R.error("登录失败");
         }
         if (!password.equals(one.getPassword())){
             return R.error("密码错误");
         }
         if (one.getStatus()==0){
             return R.error("账号已禁用");
         }
         HttpSession session = request.getSession();
         //这里属性的key有关联性的，后面会用到，所以不要乱写
         session.setAttribute("employee",one.getId());
         return R.success(one);
     }

    /**
     * 退出按钮--退出登录
     */
      @PostMapping("/logout")
      public R<String>logOut(HttpServletRequest request){
         request.getSession().removeAttribute("employee");
         return R.success("退出成功");
      }
    /**
     *  添加员工
     *  说白了就是封装对象往数据库里丢
     */
      @PostMapping
       public R<String> addEmpolyee(HttpServletRequest request,@RequestBody Employee employee){
          //设置初始密码
          String password ="123456";
          String s = DigestUtils.md5DigestAsHex(password.getBytes());
          employee.setPassword(s);
          //设置创建时间
//          employee.setCreateTime(LocalDateTime.now());
//          employee.setUpdateTime(LocalDateTime.now());
          //设置管理员
          Long id = (Long) request.getSession().getAttribute("employee");
//          employee.setCreateUser(id);
//          employee.setUpdateUser(id);
          boolean save = employeeService.save(employee);
          if (save){
              return R.success("添加成功");
          }
          return R.error("添加失败");
      }

    /**
     * 模糊查询和分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
      @GetMapping("/page")
      public R<Page> pages(int page, int pageSize, String name){
          log.info(page+" "+pageSize+" "+name);
          Page<Employee> pageInfo = new Page(page, pageSize);
          //条件构造器
          LambdaQueryWrapper<Employee> employeeLambdaQueryWrapper = new LambdaQueryWrapper<>();
          //添加条件
          employeeLambdaQueryWrapper.like(!StringUtils.isEmpty(name),Employee::getName,name);
          //排序条件
          employeeLambdaQueryWrapper.orderByDesc(Employee::getUpdateTime);
          //执行查询
          employeeService.page(pageInfo,employeeLambdaQueryWrapper);
          return R.success(pageInfo);
      }

    /**
     * 改操作，对员工状态进行更改,封装对象执行update语句
     * @param request
     * @param employee
     * @return
     */
      @PutMapping
      public R<String> update(HttpServletRequest request,@RequestBody Employee employee){
          log.info(employee.toString());
          employeeService.updateById(employee);
          return R.success("修改成功");
      }
      @GetMapping("/{id}")
      public R<Employee> getById(@PathVariable Long id){
          Employee employee = employeeService.getById(id);
          if (employee!=null){
              return R.success(employee);
          }
          return R.error("没有查询到员工信息");
      }

}
